import time
import requests
import re
import numpy as np
import pandas as pd
#同一地区不同岗位收入差异
# 不同地区同一岗位收入差异
# 工作经验和薪资的关系
# 地区的平均薪资水平
# 同一城市不同区的薪资差异
# 不同学历薪资差异
# 不同薪资段对于要掌握的技术的关系
# 某一项专业技能和薪资的关系、
# 我国各地区工资分布
# 不同规模公司的薪资水平
# 不同规模公司对岗位的需求
#https://www.51job.com/


import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Firefox(executable_path=r'D:\bianchengerbianchenger\driver\geckodriver.exe')
driver.get('https://www.zhaopin.com/')
driver.maximize_window()
# driver.maximize_window()

matrix = np.zeros((10000,9))

def get_message_of(content_you_search,page):
    # 登陆
    driver.find_element(By.XPATH, '//*[@id="zpPassportWidget"]/div/div/div/div/div[1]/div/div').click()
    time.sleep(8)
    # 搜索
    driver.find_element(By.CLASS_NAME, 'a-input__native').send_keys(content_you_search)
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[@id="rightNav_top"]/div/div[2]/div/div/div[2]/button').click()
    time.sleep(2)
    # 操作第二个界面
    cls = driver.window_handles
    time.sleep(1)
    driver.switch_to.window(cls[1])
    time.sleep(1)



    final_job_name = list()
    final_sal = list()
    final_ac = list()
    final_location = list()
    final_work_type = list()
    final_education = list()
    final_com = list()
    final_dc_at = list()
    final_renshu = list()
    final_date = list()

    for i in range(page):
        print('----------------------'+str(i)+'----------------------------------')
        time.sleep(3)
        all_div = driver.find_element(By.CSS_SELECTOR, 'div.sou-main__list').find_elements(By.CSS_SELECTOR,
                                                                                           'div.joblist-box__item.clearfix')
        for i in all_div:
            job_name = i.find_element(By.CSS_SELECTOR, 'span.iteminfo__line1__jobname__name').text
            sal = i.find_element(By.CSS_SELECTOR, 'p.iteminfo__line2__jobdesc__salary').text

            ac = i.find_element(By.CSS_SELECTOR, 'ul.iteminfo__line2__jobdesc__demand')   #重新定位
            location = ac.find_element(By.CSS_SELECTOR, 'li').text

            work_type = ac.find_element(By.CSS_SELECTOR, 'li+li').text
            education = ac.find_element(By.CSS_SELECTOR, 'li+li+li').text
            com = i.find_element(By.CSS_SELECTOR, 'span.iteminfo__line1__compname__name').text

            ab = i.find_element(By.CSS_SELECTOR, 'div.iteminfo__line2__compdesc')   #重新定位
            try:
                dc_at = ab.find_element(By.CSS_SELECTOR, 'span').text
            except Exception:
                dc_at = None
            try:
                renshu = ab.find_element(By.CSS_SELECTOR, 'span+span').text
            except Exception:
                renshu = None
            date = i.find_element(By.CSS_SELECTOR, 'span.iteminfo__line3__status__recruit').text
            print(job_name, sal, location, work_type, education, com, dc_at, renshu, date)


            final_job_name.append(job_name)
            final_sal.append(sal)
            final_location.append(location)
            final_work_type.append(work_type)
            final_education.append(education)
            final_com.append(com)
            final_dc_at.append(dc_at)
            final_renshu.append(renshu)
            final_date.append(date)

            matrix=np.vstack((final_job_name,final_sal,final_location,final_work_type,final_education,final_com
                              ,final_dc_at,final_renshu,final_date))

        time.sleep(3)

        bt = driver.find_element(By.CSS_SELECTOR,'button.btn.soupager__btn')
        driver.execute_script("arguments[0].click();",bt) #点击下一页

    return matrix

list1 = get_message_of('软件测试',4)
print(list1)
print(type(list1))
list2 = pd.DataFrame(list1)
print(list2)
list3 = pd.DataFrame(list2.values.T)
list3.to_csv('data.csv')

